# -*- coding: utf-8 -*-
from .BaseHandler import BaseHandler
import hashlib
import time
import xmltodict


class WechatHandler(BaseHandler):
    def get(self):
        """开发者验证接口"""
        echostr = self.get_argument("echostr")
        self.write(echostr)


    def post(self):

        req_xml = self.request.body
        print(req_xml)
        req = xmltodict.parse(req_xml)['xml']
        print(req.get("MsgType"))
        if "text" == req.get("MsgType"):
            resp = {
                "ToUserName":req.get("FromUserName", ""),
                "FromUserName":req.get("ToUserName", ""),
                "CreateTime":int(time.time()),
                "MsgType":"text",
                "Content":req.get("Content", ""),
                "MsgId": req.get("MsgId", "")
            }
        else:
            resp = {
                "ToUserName":req.get("FromUserName", ""),
                "FromUserName":req.get("ToUserName", ""),
                "CreateTime":int(time.time()),
                "MsgType":"text",
                "Content":"I love you, itcast!",
                "MsgId": req.get("MsgId", "")
            }
        resp_xml = xmltodict.unparse({"xml":resp})
        self.write(resp_xml)